module.exports = class Api {
    axios = require("axios");
    db = require('../libs/mysql');
    cb:any;
    req:any;
    res:any;
    err:any;
    constructor(req:any,res:any,callback:any,senderror:any){
        this.cb = callback;
        this.req = req;
        this.res = res;
        this.err = senderror;
    };
    getParam(param:string, _code?:number) {
        var code = _code || 4041;
        if (typeof(this.req.query[param]) === "undefined" && typeof(this.req.body[param]) === "undefined")
            throw code;
        else if (!this.req.query[param])
            return this.req.body[param];
        else
            return this.req.query[param];
    };
    async login(){
        let usn = this.getParam("usn");
        let pwd = this.getParam("pwd");
        let sqlCmd = "select * from users where username = ? and password = ?;";
        const [rows, fields] = await this.db.query(sqlCmd,[usn,pwd]);
        this.cb(200,rows);
    }
}